package com.open.loan.utils.sms;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.net.URLEncoder;

public class SmsTry {

    public static void main(String[] args) {
        /*
        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("accesskey", accesskey);
        parameters.put("secret", accessSecret);
        parameters.put("templateName", "【禾盛】");
        parameters.put("categoryId", "2");
        parameters.put("template", "短信通知");
        parameters.put("description", "短信通知");
        String returnBack = HttpUtil.sendGet("http://api.1cloudsp.com/open/api/addTemplate", parameters);
        System.out.println(returnBack);
        JSONObject returnBackJson = JSONObject.parseObject(returnBack);
        if (0 == Integer.parseInt(String.valueOf(returnBackJson.get("ret")))) {
            String signIdStr = String.valueOf(returnBackJson.get("data"));
            JSONObject signIdJson = JSONObject.parseObject(signIdStr);
            String templateId = String.valueOf(signIdJson.get("templateId"));
            System.out.println(templateId);
        }
        */
        SmsTry smsTry = new SmsTry();
        try {
            //smsTry.addSign("【测试添加签名】", "description");
            smsTry.modifySign("31686", "【测试添加签名】", "description");
            //smsTry.addTemplate("【鑫鑫】", "3", "短信通知test", "短信通知test");
            smsTry.modifyTemplate("47777", "【鑫鑫1】", "短信通知test", "短信通知test");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //普通短信
    public void sendsms(String phone, String templateId, String params) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/api/v2/single_send");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥

        NameValuePair[] data = {
                new NameValuePair("accesskey", accesskey),
                new NameValuePair("secret", accessSecret),
                new NameValuePair("sign", "31027"),
                new NameValuePair("templateId", templateId),
                new NameValuePair("mobile", phone),
                //new NameValuePair("content", URLEncoder.encode("验证码##1314520##验证码", "utf-8"))//（示例模板：{1}您好，您的订单于{2}已通过{3}发货，运单号{4}）
                new NameValuePair("content", URLEncoder.encode(params, "utf-8"))//（示例模板：{1}您好，您的订单于{2}已通过{3}发货，运单号{4}）
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode: " + statusCode + ", body: " + postMethod.getResponseBodyAsString());

        if (statusCode == 200) {
            JSONObject returnBackJson = JSONObject.parseObject(postMethod.getResponseBodyAsString());
            if (0 == Integer.parseInt(String.valueOf(returnBackJson.get("ret")))) {
                System.out.println("Json对象===" + returnBackJson);
            }
        }
    }

    //添加签名接口
    public void addSign(String sign, String description) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/open/api/addSign");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥

        NameValuePair[] data = {
                new NameValuePair("accesskey", accesskey),
                new NameValuePair("secret", accessSecret),
                new NameValuePair("sign", sign),
                new NameValuePair("description", description)
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode: " + statusCode + ", body: " + postMethod.getResponseBodyAsString());

        if (statusCode == 200) {
            JSONObject returnBackJson = JSONObject.parseObject(postMethod.getResponseBodyAsString());
            if (0 == Integer.parseInt(String.valueOf(returnBackJson.get("ret")))) {
                System.out.println("Json对象===" + returnBackJson);
            }
        }
    }

    //修改签名接口
    public void modifySign(String id, String sign, String description) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/open/api/modifySign");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥

        NameValuePair[] data = {
                new NameValuePair("accesskey", accesskey),
                new NameValuePair("secret", accessSecret),
                new NameValuePair("id", id),
                new NameValuePair("sign", sign),
                new NameValuePair("description", description)
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode: " + statusCode + ", body: " + postMethod.getResponseBodyAsString());

        if (statusCode == 200) {
            JSONObject returnBackJson = JSONObject.parseObject(postMethod.getResponseBodyAsString());
            if (0 == Integer.parseInt(String.valueOf(returnBackJson.get("ret")))) {
                System.out.println("Json对象===" + returnBackJson);
            }
        }
    }

    //添加签名接口
    public void addTemplate(String templateName, String categoryId, String template, String description) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/open/api/addTemplate");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥

        NameValuePair[] data = {
                new NameValuePair("accesskey", accesskey),
                new NameValuePair("secret", accessSecret),
                new NameValuePair("templateName", templateName),
                new NameValuePair("categoryId", categoryId),
                new NameValuePair("template", template),
                new NameValuePair("description", description)
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode: " + statusCode + ", body: " + postMethod.getResponseBodyAsString());
    }

    //修改模板接口
    public void modifyTemplate(String id, String templateName, String template, String description) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/open/api/modifyTemplate");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥

        NameValuePair[] data = {
                new NameValuePair("accesskey", accesskey),
                new NameValuePair("secret", accessSecret),
                new NameValuePair("id", id),
                new NameValuePair("templateName", templateName),
                new NameValuePair("template", template),
                new NameValuePair("description", description)
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode: " + statusCode + ", body: " + postMethod.getResponseBodyAsString());

        if (statusCode == 200) {
            JSONObject returnBackJson = JSONObject.parseObject(postMethod.getResponseBodyAsString());
            if (0 == Integer.parseInt(String.valueOf(returnBackJson.get("ret")))) {
                System.out.println("Json对象===" + returnBackJson);
            }
        }

    }


}
